package com.guojian.sso.session;

import javax.servlet.http.HttpSessionEvent;

import javax.servlet.http.HttpSessionListener;

import org.apache.catalina.Context;
import org.apache.catalina.core.StandardContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * 
 * @author GuoJian
 *
 */
public class SessionTomcatServletWebServerFactory extends TomcatServletWebServerFactory {

	

	@Override
	protected void postProcessContext(Context context) {
		context.setManager(new SessionManager());
		if (context instanceof StandardContext) {
			StandardContext cc = (StandardContext) context;
			cc.addApplicationLifecycleListener(new MyHttpSessionListener());
		}
		context.setSessionTimeout(1);
	}

	/**
	 * 
	 * 对session进行监听,保持所有session容器中的session一致性
	 * @author guoJian
	 *
	 */
	private class MyHttpSessionListener implements HttpSessionListener {

		@Override
		public void sessionCreated(HttpSessionEvent se) {
			//cache.add(session)
			System.out.println("===MyHttpSessionListener======sessionCreated=========");
		}

		@Override
		public void sessionDestroyed(HttpSessionEvent se) {
			//cache.remove(session)
			System.out.println("===MyHttpSessionListener======sessionDestroyed=======");
		}

	}

}
